package com.dylan.springdatajpa.service.user.impl;

import com.dylan.springdatajpa.persistence.entity.User;
import com.dylan.springdatajpa.persistence.repository.UserRepository;
import com.dylan.springdatajpa.service.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户业务逻辑
 *
 * @author zhangshaojian
 * @date 2022/7/29 9:31
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Async
    @Override
    public void asyncTransactionTest() {
        log.info("当前线程名称：{}", Thread.currentThread().getName());
        User user = new User();
        user.setUsername("aa");
        user.setPassword("bb");
        userRepository.save(user);
        int num = 1 / 0;
        User user2 = new User();
        user2.setUsername("cc");
        user2.setPassword("dd");
        userRepository.save(user2);
    }
}
